package com.freedom.common.framework.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * 请求工具类
 *
 */
@Slf4j
public class RequestUtils {

    public static HttpServletRequest getRequest() {
        return Objects.isNull(getRequestAttributes()) ? null : getRequestAttributes().getRequest();
    }

    public static HttpServletResponse getResponse() {
        return Objects.isNull(getRequestAttributes()) ? null : getRequestAttributes().getResponse();
    }

    public static ServletRequestAttributes getRequestAttributes() {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        if (Objects.isNull(attributes)) {
            return null;
        }
        return (ServletRequestAttributes) attributes;
    }
}
